package com.relife.sys.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.relife.common.model.AjaxJson;
import com.relife.common.model.ClientManager;
import com.relife.common.model.Page;
import com.relife.common.utils.StringUtil;
import com.relife.core.easyui.Easyui;
import com.relife.core.entity.AppEntity;
import com.relife.core.entity.DepartmentEntity;
import com.relife.core.qbc.QbcQuery;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;



/**
 * 系统应用管理
 * 
 * @author ANT
 * @date 2016-07-01 10:47:51
 */
@Controller
@RequestMapping("sys/app")
public class AppController extends BaseController {

	@RequestMapping
	public String appList(HttpServletRequest req) {
		return "sys/app/appList";
	}

	/**
	 * 数据列表
	 * 
	 * @author WU
	 * @date 2017年3月13日 下午2:28:24
	 * @param page
	 * @param res
	 */

	@RequestMapping
	public void listData(AppEntity app, Page page, HttpServletResponse res) {
		QbcQuery qbc = new QbcQuery(app);
		service.getPage(qbc, page);
		Easyui.datagrid(page, res);
	}

	/**
	 * 
	 * @author WU
	 * @date 2017年3月13日 下午2:28:15
	 * @param req
	 * @return
	 */
	@RequestMapping
	public String app(AppEntity app, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(app.getId())) {
			app = service.get(AppEntity.class, app.getId());
		}
		req.setAttribute("page", app);
		return "sys/app/app";
	}

	/**
	 * 保存
	 * 
	 * @author WU
	 * @date 2015-11-25 14:03:13
	 * @return
	 */
	@ResponseBody
	@RequestMapping
	public AjaxJson save(AppEntity app) {
		AjaxJson j = new AjaxJson();
		try {
			service.saveOrUpdate(app);
			ClientManager.getInstance().addApp(service.get(AppEntity.class, app.getId()));
		} catch (Exception e) {
			e.printStackTrace();
			j.setException(e);
		}
		return j;
	}

	/**
	 * 删除
	 * 
	 * @author WU
	 * @date 2017年3月13日 下午2:50:23
	 * @param department
	 * @return
	 */
	@ResponseBody
	@RequestMapping
	public AjaxJson del(DepartmentEntity department) {
		AjaxJson j = new AjaxJson();
		try {
			service.delete(department);
		} catch (Exception e) {
			e.printStackTrace();
			j.setException(e);
		}
		return j;
	}

}
